{% block sw_settings_shipping_price_matrix %}
<mt-card
    :title="cardTitle"
    :is-loading="isLoading"
    class="sw-settings-shipping-price-matrix"
    position-identifier="sw-settings-shipping-price-matrix"
>
    {% block sw_settings_shipping_price_matrix_topbar_alert %}
    <mt-banner
        v-if="priceGroup.isNew"
        class="sw-settings-shipping-price-matrix__new-matrix-alert"
        variant="attention"
        :title="$tc('global.default.warning')"
        :closable="false"
    >
        {{ $tc('sw-settings-shipping.priceMatrix.newMatrixAlertMessage') }}
    </mt-banner>
    {% endblock %}
    {% block sw_settings_shipping_price_matrix_topbar %}
    <sw-container
        columns="1fr auto 35px"
        gap="0px 24px"
        class="sw-settings-shipping-price-matrix__top-container"
    >
        {% block sw_settings_shipping_price_matrix_topbar_rule_select %}
        <sw-select-rule-create
            class="sw-settings-shipping-price-matrix__top-container-rule-select"
            :rule-id="priceGroup.ruleId"
            :rule-filter="ruleFilterCriteria"
            size="small"
            :disabled="disabled || undefined"
            :placeholder="mainRulePlaceholder"
            rule-aware-group-key="shippingMethodPrices"
            :restricted-rule-ids="restrictedRuleIds"
            :restricted-rule-ids-tooltip-label="$tc('sw-settings-shipping.priceMatrix.ruleAlreadyUsedOrRestricted')"
            @save-rule="onSaveMainRule"
        />
        {% endblock %}

        {% block sw_settings_shipping_price_matrix_topbar_new_price %}
        <mt-button
            size="x-small"
            class="sw-settings-shipping-price-matrix__top-container-add-new-rule"
            variant="primary"
            :ghost="showDataGrid"
            :disabled="!showDataGrid || disabled"
            @click="onAddNewShippingPrice"
        >
            <template v-if="isRuleMatrix">
                {{ $tc('sw-settings-shipping.priceMatrix.addNewRule') }}
            </template>
            <template v-else>
                {{ $tc('sw-settings-shipping.priceMatrix.addNewShippingPrice') }}
            </template>
        </mt-button>
        {% endblock %}

        {% block sw_settings_shipping_price_matrix_topbar_context_button %}
        <sw-context-button
            :disabled="!showDataGrid || disabled"
            class="sw-settings-shipping-price-matrix__price-group-context"
        >
            {% block sw_settings_shipping_price_matrix_topbar_context_button_duplicate %}
            <sw-context-menu-item
                v-tooltip="{
                    showDelay: 300,
                    showOnDisabledElements: true,
                    message: $tc('sw-settings-shipping.priceMatrix.newMatrixAlreadyExists'),
                    disabled: !newPriceMatrixExists
                }"
                :disabled="newPriceMatrixExists"
                class="sw-settings-shipping-price-matrix__action-duplicate"
                @click="$emit('duplicate-price-matrix', priceGroup)"
            >
                {{ $tc('sw-settings-shipping.priceMatrix.duplicateMatrixContextLabel') }}
            </sw-context-menu-item>
            {% endblock %}
            {% block sw_settings_shipping_price_matrix_topbar_context_button_delete %}
            <sw-context-menu-item
                class="sw-settings-shipping-price-matrix__action-delete"
                variant="danger"
                @click="onDeletePriceMatrix"
            >
                {{ $tc('sw-settings-shipping.priceMatrix.deleteMatrixContextLabel') }}
            </sw-context-menu-item>
            {% endblock %}
        </sw-context-button>
        {% endblock %}
    </sw-container>
    {% endblock %}

    <template #grid>
        {% block sw_settings_shipping_price_matrix_price_grid %}
        <sw-data-grid
            v-if="showDataGrid"
            :data-source="prices"
            :columns="ruleColumns"
            :show-selection="false"
            :show-settings="true"
            :compact-mode="true"
        >
            {% block sw_settings_shipping_price_matrix_price_grid_column_calculcation_rule %}
            <template
                #column-calculationRule="{ item, itemIndex, compact }"
            >
                <sw-select-rule-create
                    class="sw-settings-shipping-price-matrix__column-calculation-rule-select"
                    size="small"
                    :disabled="disabled"
                    :rule-id="item.calculationRuleId"
                    :rule-filter="shippingRuleFilterCriteria"
                    :placeholder="$tc('sw-settings-shipping.priceMatrix.chooseCustomRule')"
                    rule-aware-group-key="shippingMethodPriceCalculations"
                    :restricted-rule-ids="usedCalculationRules"
                    :restricted-rule-ids-tooltip-label="$tc('sw-settings-shipping.priceMatrix.ruleAlreadyUsedInMatrix')"
                    @save-rule="(ruleId) => onSaveCustomShippingRule(ruleId, item)"
                >
                    <template #rule-modal="{ showRuleModal, onSaveRule, onCloseRuleModal }">
                        <sw-price-rule-modal
                            v-if="showRuleModal"
                            rule-aware-group-key="shippingMethodPriceCalculations"
                            @save="onSaveRule"
                            @modal-close="onCloseRuleModal"
                        />
                    </template>
                </sw-select-rule-create>
            </template>
            {% endblock %}
            {% block sw_settings_shipping_price_matrix_price_grid_column_quantity_start %}
            <template
                #column-quantityStart="{ item, itemIndex, compact }"
            >
                <mt-number-field
                    v-model="item.quantityStart"
                    :disabled="disabled"
                    :name="`sw-field--${item.id}-quantity-start`"
                    :number-type="numberFieldType"
                    :size="compact ? 'small' : 'default'"
                    :min="priceGroup.prices[itemIndex - 1] ? priceGroup.prices[itemIndex - 1].quantityEnd : 0"
                    :max="item.quantityEnd ? item.quantityEnd : null"
                    :digits="3"
                />
            </template>
            {% endblock %}
            {% block sw_settings_shipping_price_matrix_price_grid_column_quantity_end %}
            <template
                #column-quantityEnd="{ item, itemIndex, compact }"
            >
                <mt-number-field
                    v-model="item.quantityEnd"
                    :disabled="disabled"
                    :name="`sw-field--${item.id}-quantity-end`"
                    :number-type="numberFieldType"
                    :size="compact ? 'small' : 'default'"
                    :placeholder="$tc('sw-settings-shipping.priceMatrix.any')"
                    :validation="item.quantityEnd === null || item.quantityEnd > item.quantityStart"
                    :min="item.quantityStart"
                    :digits="3"
                    @update:model-value="onQuantityEndChange(item)"
                />
            </template>
            {% endblock %}
            {% block sw_settings_shipping_price_matrix_price_grid_currencies_list %}
            <template
                v-for="currency in currencies"
                :key="currency.isoCode"
                #[`column-price-${currency.isoCode}`]="{ item, column, compact }"
            >
                <sw-inherit-wrapper
                    class="sw-settings-shipping-price-matrix__price"
                    :value="getPrice(item, currency)"
                    :has-parent="!currency.isSystemDefault"
                    :inherited-value="currency.isSystemDefault ? null : convertDefaultPriceToCurrencyPrice(item, currency)"
                    @update:value="setPrice(item, currency, $event)"
                >
                    <template #content="props">

                        <sw-inheritance-switch
                            v-if="!currency.isSystemDefault"
                            class="sw-settings-shipping-price-matrix__price-inherit-icon"
                            :is-inherited="props.isInherited"
                            :disabled="disabled"
                            @inheritance-restore="props.restoreInheritance"
                            @inheritance-remove="props.removeInheritance"
                        />

                        <mt-number-field
                            v-model="props.currentValue.gross"
                            :name="`sw-field--${item.id}-${currency.id}-gross`"
                            :size="compact ? 'small' : 'default'"
                            class="sw-settings-shipping-price-matrix__price-input"
                            :digits="50"
                            :disabled="props.isInherited || disabled"
                        />
                        <mt-number-field
                            v-model="props.currentValue.net"
                            :name="`sw-field--${item.id}-${currency.id}-net`"
                            :size="compact ? 'small' : 'default'"
                            class="sw-settings-shipping-price-matrix__price-input"
                            :digits="50"
                            :disabled="props.isInherited || disabled"
                        />
                    </template>
                </sw-inherit-wrapper>
            </template>
            {% endblock %}

            {% block sw_settings_shipping_price_matrix_price_grid_actions %}
            <template #actions="{ item }">
                {% block sw_settings_shipping_price_matrix_price_grid_actions_delete %}
                <sw-context-menu-item
                    :disabled="disableDeleteButton || disabled"
                    variant="danger"
                    @click="onDeleteShippingPrice(item)"
                >
                    {{ $tc('sw-settings-shipping.priceMatrix.contextMenuDeletePrice') }}
                </sw-context-menu-item>
                {% endblock %}
            </template>
            {% endblock %}
        </sw-data-grid>
        <div
            v-if="showDataGrid && !showAllPrices"
            class="sw-settings-shipping-price-matrix__price-load-all"
        >
            <mt-button
                ghost
                size="small"
                class="sw-settings-shipping-price-matrix__price-load-all-button"
                variant="secondary"
                @click="updateShowAllPrices"
            >
                {{ $tc('sw-settings-shipping.priceMatrix.buttonLoadAllPrices') }}
            </mt-button>
        </div>
        {% endblock %}

        {% block sw_settings_shipping_price_matrix_empty %}
        <div
            v-if="!showDataGrid"
            class="sw-settings-shipping-price-matrix__empty"
        >
            {% block sw_settings_shipping_price_matrix_empty_text %}
            <p class="sw-settings-shipping-price-matrix__empty-text">
                {{ $tc('sw-settings-shipping.priceMatrix.emptyText') }}
            </p>
            {% endblock %}
            <sw-container
                columns="1fr 10px 1fr"
                gap="0px 24px"
            >
                {% block sw_settings_shipping_price_matrix_empty_property_store %}
                <sw-single-select
                    class="sw-settings-shipping-price-matrix__empty--select-property"
                    :options="calculationTypes"
                    :value="priceGroup.calculation"
                    :disabled="disabled"
                    size="small"
                    show-clearable-button
                    :placeholder="$tc('sw-settings-shipping.priceMatrix.selectProperty')"
                    @update:value="onCalculationChange"
                />
                {% endblock %}

                <p class="sw-settings-shipping-price-matrix__empty-text-or">
                    {{ $tc('sw-settings-shipping.priceMatrix.textOr') }}
                </p>

                {% block sw_settings_shipping_price_matrix_empty_rule_select %}
                <sw-select-rule-create
                    size="small"
                    :disabled="disabled"
                    :rule-filter="shippingRuleFilterCriteria"
                    :placeholder="$tc('sw-settings-shipping.priceMatrix.chooseCustomRule')"
                    rule-aware-group-key="shippingMethodPriceCalculations"
                    :restricted-rule-ids="usedCalculationRules"
                    :restricted-rule-ids-tooltip-label="$tc('sw-settings-shipping.priceMatrix.ruleAlreadyUsedInMatrix')"
                    @save-rule="(ruleId) => { onSaveCustomShippingRule(ruleId) }"
                >
                    <template #rule-modal="{ showRuleModal, onSaveRule, onCloseRuleModal }">
                        <sw-price-rule-modal
                            v-if="showRuleModal"
                            rule-aware-group-key="shippingMethodPriceCalculations"
                            @save="onSaveRule"
                            @modal-close="onCloseRuleModal"
                        />
                    </template>
                </sw-select-rule-create>
                {% endblock %}
            </sw-container>
        </div>
        {% endblock %}
    </template>

    {% block sw_settings_shipping_price_matrix_delete_modal %}
    <sw-modal
        v-if="showDeleteModal"
        :title="$tc('global.default.warning')"
        variant="small"
        @modal-close="onCloseDeleteModal"
    >
        {% block sw_settings_shipping_price_matrix_delete_modal_confirm_delete_text %}
        <p class="sw-settings-shipping-price-matrix__confirm-delete-text">
            {{ confirmDeleteText }}
        </p>
        {% endblock %}

        {% block sw_settings_shipping_price_matrix_delete_modal_footer %}
        <template #modal-footer>
            {% block sw_settings_shipping_price_matrix_delete_modal_cancel %}
            <mt-button
                size="small"
                variant="secondary"
                @click="onCloseDeleteModal"
            >
                {{ $tc('global.default.cancel') }}
            </mt-button>
            {% endblock %}

            {% block sw_settings_shipping_price_matrix_delete_modal_confirm %}
            <mt-button
                variant="critical"
                size="small"
                @click="onConfirmDeleteShippingPrice"
            >
                {{ $tc('sw-settings-shipping.priceMatrix.buttonDelete') }}
            </mt-button>
            {% endblock %}
        </template>
        {% endblock %}
    </sw-modal>
    {% endblock %}
</mt-card>
{% endblock %}
